import GlobalStore from '../../global.store';
import {observable, action, runInAction} from 'mobx';
import Parse from 'parse';

const ThesisParse = Parse.Object.extend('Thesis');

export default class ThesisStore extends GlobalStore{
  @observable defaultPage = 1;
  @observable defaultSize = 10;
  @observable total = 0;
  @observable page = 1;
  @observable size = 10;
  @observable list = [];
  @observable editFormVisible = false;
  @observable editThesis = {};
  @observable editState = 'add';
  @observable searchParams = {};
  @observable isSearch = false;

  // 查询列表
  @action getList = async (page, size) => {
    this.openLoading();
    this.page = page || this.defaultPage;
    this.size = size || this.defaultSize;
    let skip = (page - 1) * size;

    let query = new Parse.Query(ThesisParse);
    let total = await query.count();
    if(this.isSearch) {
      console.log(this.searchParams);
      for(let i in this.searchParams) {
        // console.log(i, this.searchParams[i]);
        query.contains(i, this.searchParams[i]);
      }
    }
    let result = await query.skip(skip).limit(this.size).descending('createdAt').find();
    let list = [];
    result.forEach(item => {
      list.push(item.toJSON());
    });
    runInAction(() => {
      this.loading = false;
      this.total = total;
      this.list = list;
    });
  };

  // 删除用户
  @action del = async (keys) => {
    this.openLoading();
    let thesisList = [];
    keys.forEach(key => {
      let thesisModel = new ThesisParse();
      thesisModel.set('objectId', key);
      thesisList.push(thesisModel);
    })
    let result = await Parse.Object.destroyAll(thesisList);
    console.log('keys: ', keys, 'result: ', result);
    runInAction(() => {
      this.loading = false;
      this.getList(this.page, this.size);
    });
  }

  // 显示编辑框
  @action showEdit = () => {
    this.editFormVisible = true;
  }

  // 隐藏编辑框
  @action hideEdit = () => {
    this.editFormVisible = false;
  }

  // 设置当前编辑的用户
  @action setEditThesis = (thesis) => {
    this.editThesis = thesis || {};
    this.editState = thesis ? 'edit' : 'add';
  }

  // 编辑内容
  @action edit = async (thesis) => {
    this.openLoading();
    console.log('start: ', thesis);
    let thesisModel = new ThesisParse();
    let result = null;
    switch(this.editState) {
      case 'add':
        // for(let i in thesis) {
        //   thesisModel.set(i, thesis[i])
        // }
        result = await thesisModel.save(thesis);
        console.log('add result: ', result);
        break;
      case 'edit':
        thesis.objectId = this.editThesis.objectId;
        result = await thesisModel.save(thesis);
        console.log('update result: ', result);
        break;
      default:
        return;
    }
    runInAction(() => {
      this.loading = false;
      // this.setEditThesis();
      this.getList(this.page, this.size);
    });
  }

  // 搜索内容
  @action search = async (params) => {
    console.log(params, 'search');
    this.searchParams = params;
    this.isSearch = Object.keys(params).length !== 0;
    this.getList();
  }
}